package com.tg.data.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.appbase.custom.base.DeviceFeatureSettings;
import com.appbase.custom.constant.DeviceStateConstants;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGLog;

import java.util.Map;

import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;
import io.objectbox.annotation.Transient;

/**
 * Describe:
 * Created by Rance Rang on 2019/1/22.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
@Entity
public class DeviceSettingsInfo implements Parcelable {

    public static final String SETTINGS_INFO_SDCARD_FREE = "SDFree";
    public static final String SETTINGS_INFO_SDCARD_TOTAL = "SDTotal";
    public static final String SETTINGS_INFO_DEVICE_TYPE = "DeviceType";
    public static final String SETTINGS_INFO_VENDOR = "vendor";
    public static final String SETTINGS_INFO_VERSION = "version";
    public static final String SETTINGS_INFO_STREAM = "streamQuality";
    public static final String SETTINGS_INFO_SDRECORD = "SDRecordMode";//sd录像模式
    public static final String SETTINGS_INFO_MIRROR = "mirrorMode";//画面翻转
    public static final String SETTINGS_INFO_FREQUNENCY = "PowerFrequency"; //防闪烁 、频率
    public static final String SETTINGS_INFO_TRACK = "trackMode";
    public static final String SETTINGS_INFO_NIGHT_VISION = "nightVision";
    public static final String SETTINGS_INFO_DOUBLE_LIGHT_MODE = "doubleLightMode";
    public static final String SETTINGS_INFO_DOUBLE_LIGHT = "doubleLightStatus";
    public static final String SETTINGS_INFO_CLOUD_VIDEO_QUALITY = "CVideoQuality";  //云视频录像分辨率
    public static final String SETTINGS_INFO_MICROPHONE = "Microphone";  //麦克风
    public static final String SETTINGS_INFO_BUZZER = "Buzzer";  //警铃
    public static final String SETTINGS_INFO_ALERTSOUND = "AlertSound";  //警铃
    public static final String SETTINGS_INFO_MOTION_DETECTION = "MD-Capabilities";  //移动侦测
    public static final String SETTINGS_INFO_DEVICE_STATUS = "DeviceStatus";  //开关
    public static final String SETTINGS_INFO_MULTICHANNELS = "MultiChannels";  //开关
    public static final String SETTINGS_INFO_RECORDCONF = "RecordConf";
    public static final String SETTINGS_INFO_RECORDCONF_VALUS = "res";
    public static final String SETTINGS_INFO_RECORDCONF_NO_MEDIA_VALUS = "no-media";

    public static final String SETTINGS_INFO_TIME_LAPSE_VALUS = "time-lapse";

    @Id
    public long id;
    public long deviceID;
    public String current_version_code;
    public String firmware_id;
    public  String uuid;
    public  int free;
    public  int total = -999;
    public  String mode;  //ipc
    public  String version;//版本号
    public   String vendor;   // oem
    public  int envMode;//防闪烁
    public  int quality;//视频质量
    public  int recordType;//录像模式
    public int  videoMode;//视频旋转
    public String timezone; //时区
    public  int nightVision;//夜视
    public  int autoTracking;//自动追踪
    public int doubleLightMode ;//双光支持类型
    public int doubleLight;//双光
    public int cloudResolution; //云录像清晰度
    public int microphoneOn = 1; //设备麦克风
    public int timeLapseRecordOn; //缩时录影
    public int  buzzerOn; //设备警铃
    @Transient
    public boolean online;
    @Transient
    public boolean ai_service;
    @Transient
    public boolean isCar;
    public long updated_at;
    public long sleepTime;// 4g设备休眠时间，播放多久进入休眠
    public String areaAlarmPlanTime;

    @Transient
    public int deviceStatus = 1;

    @Transient
    public boolean hasDeviceUpdate;//是否固件更新
    public int sensitivityLevel = 0;
    public int carParkingMonitoringSensitivityLevel = 0; // 停车监控灵敏度s
    public String device_type;
    public int batteryPower = 0;
    public byte recordStream = 0;

    public int max_awake_time;
    public int speakerVolume;
    public int micVolume = 0;
    public int pir;
    public int alarmLightOn;
    public int ledStatusOn;
    public int aiOn;
    public float zoomPos;
    public int doubleLightNight = -1;//双光
    public int sensitivity = 0;//
    public boolean isSupportPresetPoint;
    public boolean isDefenceAudioClosed;
    public  byte[] motionDetect;
    public int bodyDetectionFrame;
    public boolean supportTemper = false;
    public boolean supportHumidity = false;
    public float zoomPos2;
    public  int quality2;//视频质量
    public boolean noMedia;
    public int maxPresetPoint = -1;
    public int maxTracks = 2;
    public int tracksType = -1;
    public int orderId = -1;

    public int deviceBroadcast; //设备麦克风
    public DeviceSettingsInfo(){
    }


    protected DeviceSettingsInfo(Parcel in) {
        id = in.readLong();
        deviceID = in.readLong();
        current_version_code = in.readString();
        firmware_id = in.readString();
        uuid = in.readString();
        free = in.readInt();
        total = in.readInt();
        mode = in.readString();
        version = in.readString();
        vendor = in.readString();
        envMode = in.readInt();
        quality = in.readInt();
        recordType = in.readInt();
        videoMode = in.readInt();
        timezone = in.readString();
        nightVision = in.readInt();
        autoTracking = in.readInt();
        doubleLightMode = in.readInt();
        doubleLight = in.readInt();
        cloudResolution = in.readInt();
        microphoneOn = in.readInt();
        buzzerOn = in.readInt();
        online = in.readByte() != 0;
        ai_service = in.readByte() != 0;
        isCar = in.readByte() != 0;
        updated_at = in.readLong();
        sleepTime = in.readLong();
        areaAlarmPlanTime = in.readString();
        deviceStatus = in.readInt();
        hasDeviceUpdate = in.readByte() != 0;
        sensitivityLevel = in.readInt();
        carParkingMonitoringSensitivityLevel = in.readInt();
        device_type = in.readString();
        batteryPower = in.readInt();
        recordStream = in.readByte();
        max_awake_time = in.readInt();
        speakerVolume = in.readInt();
        micVolume = in.readInt();
        pir = in.readInt();
        alarmLightOn = in.readInt();
        ledStatusOn = in.readInt();
        aiOn = in.readInt();
        zoomPos = in.readFloat();
        doubleLightNight = in.readInt();
        sensitivity = in.readInt();
        isSupportPresetPoint = in.readByte() != 0;
        isDefenceAudioClosed = in.readByte() != 0;
        motionDetect = in.createByteArray();
        bodyDetectionFrame = in.readInt();

        supportTemper = in.readByte() != 0;
        supportHumidity = in.readByte() != 0;
        zoomPos2 = in.readFloat();
        quality2 = in.readInt();

        noMedia = in.readByte() != 0;

        maxPresetPoint = in.readInt();
        maxTracks = in.readInt();
        tracksType = in.readInt();
        orderId = in.readInt();

        deviceBroadcast = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
        dest.writeLong(deviceID);
        dest.writeString(current_version_code);
        dest.writeString(firmware_id);
        dest.writeString(uuid);
        dest.writeInt(free);
        dest.writeInt(total);
        dest.writeString(mode);
        dest.writeString(version);
        dest.writeString(vendor);
        dest.writeInt(envMode);
        dest.writeInt(quality);
        dest.writeInt(recordType);
        dest.writeInt(videoMode);
        dest.writeString(timezone);
        dest.writeInt(nightVision);
        dest.writeInt(autoTracking);
        dest.writeInt(doubleLightMode);
        dest.writeInt(doubleLight);
        dest.writeInt(cloudResolution);
        dest.writeInt(microphoneOn);
        dest.writeInt(buzzerOn);
        dest.writeByte((byte) (online ? 1 : 0));
        dest.writeByte((byte) (ai_service ? 1 : 0));
        dest.writeByte((byte) (isCar ? 1 : 0));
        dest.writeLong(updated_at);
        dest.writeLong(sleepTime);
        dest.writeString(areaAlarmPlanTime);
        dest.writeInt(deviceStatus);
        dest.writeByte((byte) (hasDeviceUpdate ? 1 : 0));
        dest.writeInt(sensitivityLevel);
        dest.writeInt(carParkingMonitoringSensitivityLevel);
        dest.writeString(device_type);
        dest.writeInt(batteryPower);
        dest.writeByte(recordStream);
        dest.writeInt(max_awake_time);
        dest.writeInt(speakerVolume);
        dest.writeInt(micVolume);
        dest.writeInt(pir);
        dest.writeInt(alarmLightOn);
        dest.writeInt(ledStatusOn);
        dest.writeInt(aiOn);
        dest.writeFloat(zoomPos);
        dest.writeInt(doubleLightNight);
        dest.writeInt(sensitivity);
        dest.writeByte((byte) (isSupportPresetPoint ? 1 : 0));
        dest.writeByte((byte) (isDefenceAudioClosed ? 1 : 0));
        dest.writeByteArray(motionDetect);
        dest.writeInt(bodyDetectionFrame);
        dest.writeByte((byte) (supportTemper ? 1 : 0));
        dest.writeByte((byte) (supportHumidity ? 1 : 0));
        dest.writeFloat(zoomPos2);
        dest.writeInt(quality2);
        dest.writeByte((byte) (noMedia ? 1 : 0));
        dest.writeInt(maxPresetPoint);
        dest.writeInt(maxTracks);
        dest.writeInt(tracksType);
        dest.writeInt(orderId);
        dest.writeInt(deviceBroadcast);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<DeviceSettingsInfo> CREATOR = new Creator<DeviceSettingsInfo>() {
        @Override
        public DeviceSettingsInfo createFromParcel(Parcel in) {
            return new DeviceSettingsInfo(in);
        }

        @Override
        public DeviceSettingsInfo[] newArray(int size) {
            return new DeviceSettingsInfo[size];
        }
    };

    public static DeviceSettingsInfo updateData(DeviceSettingsInfo old , DeviceSettingsInfo newInfo , DeviceItem mDevice){
        if (newInfo != null) {
            newInfo.deviceID = mDevice.id;
            newInfo.uuid = mDevice.uuid;
            newInfo.id = old.id;
            newInfo.online = (mDevice.is_online == DeviceStateConstants.DEVICE_ONLINE);
            newInfo.current_version_code = mDevice.current_version_code;
            newInfo.firmware_id = mDevice.firmware_id;
            newInfo.timezone = mDevice.timezone;
            newInfo.sleepTime = old.sleepTime;

            if (old.quality != 0){
                newInfo.quality = old.quality;
            }
            if (old.quality2 != 0){
                newInfo.quality2 = old.quality2;
            }
            if (newInfo.quality2 == 0){
                newInfo.quality2 = newInfo.quality;
            }
            newInfo.isCar = old.isCar;
            newInfo.maxPresetPoint = old.maxPresetPoint;
            newInfo.maxTracks = old.maxTracks;
            newInfo.tracksType = old.tracksType;
            newInfo.sensitivityLevel = old.sensitivityLevel;
            newInfo.carParkingMonitoringSensitivityLevel = old.carParkingMonitoringSensitivityLevel;
            newInfo.mode = old.mode;
            newInfo.doubleLightNight = old.doubleLightNight;
            newInfo.isSupportPresetPoint = old.isSupportPresetPoint;
        }
        return newInfo;
    }
    public static  DeviceSettingsInfo  newDeviceSettingsInfo(DeviceFeatureSettings.DeviceFeatureSettingsResp resp){
        if (resp==null||resp.setting==null)return null;
        DeviceSettingsInfo  info = new DeviceSettingsInfo();

        for (Map.Entry<String, String> entry : resp.setting.entrySet()) {
            String keys = entry.getKey();
            String values = entry.getValue();
            switch (keys){
                case SETTINGS_INFO_RECORDCONF:
                    info.noMedia = StringUtils.equalsIgnoreCase("no-media", values);
                    break;
                case  SETTINGS_INFO_SDCARD_TOTAL:
                    info.total = info.getSettingInt(values);
                    break;
                case  SETTINGS_INFO_SDCARD_FREE:
                    info.free = info.getSettingInt(values);
                    break;
                case  SETTINGS_INFO_STREAM:
                    info.quality = info.getSettingInt(values);
                    break;
                case  SETTINGS_INFO_FREQUNENCY:
                    info.envMode = info.getSettingInt(values);
                    break;
                case  SETTINGS_INFO_VENDOR:
                    info.vendor = values.trim();
                    break;
                case  SETTINGS_INFO_DOUBLE_LIGHT:
                    info.doubleLight = info.getSettingInt(values);
                    break;

                case  SETTINGS_INFO_NIGHT_VISION:
                    info.nightVision = info.getSettingInt(values);
                    break;
                case  SETTINGS_INFO_TRACK:
                    info.autoTracking = info.getSettingInt(values);
                    break;
                case  SETTINGS_INFO_MIRROR:
                    info.videoMode = info.getSettingInt(values);
                    break;
                case  SETTINGS_INFO_SDRECORD:
                    info.recordType = info.getSettingInt(values);
                    TGLog.d("VideoType", "recordType1 = " + info.recordType);
                    break;
                case  SETTINGS_INFO_CLOUD_VIDEO_QUALITY:
                    info.cloudResolution =  info.getSettingInt(values);
                    break;
                case  SETTINGS_INFO_VERSION:
                    info.version =  values.trim();
                    break;
                case  SETTINGS_INFO_MICROPHONE:
                    info.microphoneOn =   info.getSettingInt(values);
                    TGLog.d("CameraViewActivity",  "info.microphoneOn = " + info.microphoneOn);
                    break;
                case  SETTINGS_INFO_BUZZER:
                    info.buzzerOn =   info.getSettingInt(values);;
                    break;
                case  SETTINGS_INFO_ALERTSOUND:
                    info.buzzerOn =   info.getSettingInt(values);;
                    break;
                case  SETTINGS_INFO_DEVICE_STATUS:
                    info.deviceStatus =   info.getSettingInt(values);
                    break;
            }

        }
        return info;
    }
    private int getSettingInt(String str) {
        if (TextUtils.isEmpty(str)) return 0;
        return Integer.parseInt(str);
    }

}
